home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / MCASM.RAR / MC_ASM.EXE / WROX_ASM / CH12 / COMMON / GRFILE.H < prev    next >
C/C++ Source or Header  |  1994-09-24  |  6KB  |  170 lines

  1. #ifndef GFILE_H
  2. #define GFILE_H
  3. #include <stdio.h>
  4. #include "common.h"
  5. #include "manager.h"
  6. #include "graph.h"
  7.  
  8. /* graph_file reader and saver - the actual father of all image format classes
  9.  * It supports line-by-line image reading and writing
  10.  * (But not the whole image at once).
  11.  * This feature cause some difficulties for us (see formats.cpp)
  12.  *     and, possibly, for you; but it solves problem of
  13.  *
  14.  *    !! NOT ENOUGH MEMORY !!!
  15.  *
  16.  * Note: you can only sequentially read the lines of image,
  17.  * normally (for compressed images) it is too difficult
  18.  * to jump to random line #
  19.  *
  20.  * Written by E. Podvoysky & Kiselev J. CZ 1994.
  21.  */
  22.  
  23.  
  24.  
  25. #define BUFF_SIZE 30000 // size of object's input buffer
  26. enum image_type {MONOIMG,COLOR16IMG,GRAY256IMG,COLOR256IMG,TRUECOLORIMG};
  27. enum image_extention_type {NO_EXTENTION,
  28.     MONOGIF, // versus NO_EXTENTION
  29.     COLOR16_2PIXEL,COLOR16_1PIXEL,COLOR16_PLANE,
  30.     GRAY_PALETTE, // versus NO_EXTENTION
  31.     RGB15,RGB16,BGR24,RGB24,BGRA32,RGBPLANE24};
  32.  
  33. /* image forms that are used in formats and in our procedures: */
  34. /*  MONOIMG,NO_EXTENTION - bit per pixel
  35.  *  MONOIMG,MONOGIF - byte (though only 0 or 1 in value) per pixel
  36.  *  COLOR16IMG, COLOR16_2PIXEL - two pixels in each byte
  37.  *             (most significant bits - left pixel)
  38.  *  COLOR16IMG, COLOR16_1PIXEL - byte (value range: 0..15) per pixel
  39.  *  COLOR16IMG, COLOR16_PLANE - four planes of one dispaly row
  40.  *        are stored in one array (16 colors EGA/VGA video mode)
  41.  *  GRAY256IMG, NO_EXTENTION - byte (256 shades) per pixel
  42.  *  GRAY256IMG, GRAY_PALETTE - byte per pixel, shades are defined in palette
  43.  *          absolutely the same as COLOR256IMG
  44.  *  COLOR256IMG - byte per pixel (256 colors)
  45.  *  TRUECOLORIMG RGB15 - high color 32k colors
  46.  *  TRUECOLORIMG RGB16 - high color 64k colors
  47.  *  TRUECOLORIMG RGB24 - true color,
  48.  *  TRUECOLORIMG BGRA32 - B, G, R and Alfa - 4 bytes per pixel (see Targa format descr.)
  49.  *  TRUECOLORIMG RGBPLANE24 - all line's pixels R values, then G values and B.
  50.  */
  51.  
  52. int get_bitsperpixel(image_type it,image_extention_type iet);
  53.  
  54. extern long minheap;
  55.     // minimal size of heap which is to be free (normally 70000L);
  56.  
  57. class graph_file_abstract { // some graphics format file reader are
  58. protected:
  59.     FILE *the_file;
  60.     BYTE *buffer;
  61.     WORD buff_offset;
  62.     BOOL usebuffer;
  63.     void (*line2standart)(BYTE*,BYTE*,int);
  64. public:
  65.     BOOL file_ok;
  66.     BOOL top_to_bottom,left_to_right; // pixels order
  67.     int width,height;
  68.     int bytesperline;
  69.     int line_n;        //current line number
  70.     long colors;
  71.  
  72.     graph_file_abstract();
  73.     virtual ~graph_file_abstract(); // closes file and disposes buffer
  74.     long image_size();
  75.     void init_buffer(BOOL usebuffer_);
  76.     // procedure for constructors, call when file is opened!
  77.     // if (usebuffer_) allocates buffer
  78.     // otherwise - bufferizes file
  79.     // WARNING:
  80.     // in the last case procedure calls fseek(0,SEEK_SET);
  81.     // for savers  call only just after file was opened!
  82.  
  83. };
  84.  
  85. class graph_file_reader: public graph_file_abstract {
  86. protected:
  87.     long data_begin;     // don't forget to set it, descendants!!!
  88. public:
  89.     BGRpalette palette;
  90.     image_type source_type;
  91.     image_extention_type source_ext_type;
  92.  
  93.     BYTE *source_line;    // get readed line from here
  94.     BYTE *standart_line;    // get converted to our internal form
  95.                 // (by get_stand_line())  line from here
  96.  
  97.     graph_file_reader() {}; // formal
  98.     graph_file_reader(char *fname);     // opens the file
  99.  
  100.     virtual void seek_start();     // seek to image itself start
  101.                     // normally constructors calls seek_start()
  102.  
  103.     virtual void fresh_buffer();     // if buffer is almost cleared
  104.                     // reads new portion of data
  105.     int get_stand_line();    // returns -1 if file is already finished
  106.     virtual int get_next_line();    // returns -1 if file is already finished
  107. };
  108.  
  109. /* we convert data in one of 3 our internal formats
  110.  * (just for simplicity of further processing).
  111.  * MONO bit per pixel, byte per pixel and RGB lines
  112.  * You may use faster get_next_line() and get data from source_line,
  113.  * and bother yourself by CASEs for all possible image forms
  114.  * or call get_stand_line() and get converted data from standart_line
  115.  */
  116.  
  117. class graph_file_saver: public graph_file_abstract  { // some graphics format file saver
  118. protected:
  119.     BGRpalette *palette; // keeps only pointer on pallette
  120. public:
  121.     image_type dest_type;
  122.     image_extention_type dest_ext_type;
  123.  
  124.     graph_file_saver() {}; //formal
  125.     graph_file_saver(char *fname,
  126.             BOOL usebuffer_,
  127.             image_type dest_type_,
  128.             image_extention_type dest_ext_type_,
  129.             int width_,int height_,
  130.             BGRpalette* pal);
  131.         // object  keeps only pointer on pallette
  132.         // so do not change or delete  palette before delete object
  133.     virtual void update_palette(BGRpalette *pal) {};
  134.     int put_stand_line(BYTE *line);    // returns -1 if file is finished
  135.     virtual int put_next_line(BYTE *line);    //returns -1 if some error occurs
  136.     virtual void flush_buffer();     // writes acumulated buffer in file
  137. };
  138.  
  139.  
  140. /* abstract image storage, may be in file or in memory
  141.  * or, possibly in expanded memory, if you write proprer descendant
  142.  * it simply stores image line after line, and then returns
  143.  * line you want.
  144. */
  145. enum memory_type {convRAM, exRAM, DISK, UNKNOWN};
  146.  
  147. class image_keeper: public graph_file_abstract {
  148. protected:
  149.     BYTE *current_line; // is used if data are stored in file
  150.     BYTE **lines;
  151.     USER_HANDLE *lines_ext;
  152.     char *tmp_filename;
  153. public:
  154.     memory_type where;
  155.     image_type im_type;
  156.     BGRcolortype *palette;
  157.  
  158.     image_keeper(memory_type where_, // set UNKNOWN if you want kepper to select
  159.             image_type im_type_,
  160.             int width_,int height_,BGRcolortype * pal,
  161.             BOOL top_to_bottom_);
  162.     ~image_keeper();
  163.     BYTE * give_line(int line_no); // read stored line
  164.     void store_line(BYTE * source);
  165.     void replace_line(int line_no,BYTE * source);
  166.     };
  167.  
  168.  
  169. #endif
  170.